package com.qin.springbootdemo.controller;

import com.alibaba.fastjson.JSON;
import com.qin.springbootdemo.entity.cartlistEntity;
import com.qin.springbootdemo.entity.commodities;
import com.qin.springbootdemo.entity.favoriteEntity;
import com.qin.springbootdemo.entity.swiper;
import com.qin.springbootdemo.service.webService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Leisure
 * create 2022 2022/6/18 10:31
 **/
@RestController
@RequestMapping(value = "/web")
public class webController {
    @Autowired
    private webService service;

    //以下为商品
    //查询全部商品
    @RequestMapping(value = "/search-goods",method = RequestMethod.GET)
    public List<commodities> findGoodsAll()
    {
        List<commodities> list = this.service.findGoodsAll();
        return list;
    }
    //查询轮播图
    @RequestMapping(value = "/select-swiper",method = RequestMethod.GET)
    public List<swiper> findSwiper()
    {
        List<swiper> list = this.service.findSwiper();
        return list;
    }
    //商品搜索
    @RequestMapping(value = "/search-spgoods/{key}",method = RequestMethod.GET )
    @ApiOperation(value = "Search keyword by goods", notes = "")
    public List<commodities> findSpecificGoods(@ApiParam(value = "desc of param" , required=true ) @PathVariable("key") String key){
        List<commodities> list =this.service.findSpecificGoods(key);
        return  list;
    }
    //查找商品ID
    @GetMapping("/search-goods-id/{id}")
    @ApiOperation(value = "search-goods-by-id", notes = "")
    public commodities findGoodById(@ApiParam(value = "desc of param",required =true)@PathVariable("id") String id)
    {
        commodities list = this.service.findGoodById(id);
        return list;
    }


    //以下为购物车模块
    //查询购物车
    @RequestMapping(value = "/search-carts",method = RequestMethod.GET)
    public List<cartlistEntity> findCartAll(){
        List<cartlistEntity> list = this.service.findCartAll();
        return list;
    }

    //加入购物车、将对象转为JSON对象才能获取数据，才能加入购物车
    @ResponseBody
    @RequestMapping(value = "insert-cart" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "insert-cart", notes = "")
    public void insertCar(@RequestBody String str)
    {
        cartlistEntity cart = JSON.parseObject(str, cartlistEntity.class);
        this.service.insertCart(cart);
    }

    //更新购物车
    @ResponseBody
    @RequestMapping(value = "update-cart" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "update-cart", notes = "")
    public void updateCart(@RequestBody String str)
    {
        cartlistEntity cart = JSON.parseObject(str, cartlistEntity.class);
        this.service.updateCart(cart);
    }

    //删除购物车商品
    @GetMapping("/delete-cart-id/{id}")
    @ApiOperation(value = "delete-cart-id", notes = "")
    public void delCart(@ApiParam(value = "desc of param",required =true)@PathVariable("id") String id) {
        this.service.delCart(id);
    }

//以下为收藏模块
    //查询收藏
    @RequestMapping(value = "/search-favorite",method = RequestMethod.GET)
    public List<favoriteEntity> findFavoriteAll(){
        List<favoriteEntity> list = this.service.findFavoriteAll();
        return list;
    }

    //删除收藏
    @GetMapping("/delete-favorite-by-id/{id}")
    @ApiOperation(value = "delete-favorite-by-id", notes = "")
    public void delFavorite(@ApiParam(value = "desc of param",required =true)@PathVariable("id") String id) {
        this.service.delFavorite(id);
    }

    //插入收藏
    @ResponseBody
    @RequestMapping(value = "insert-favorite" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "insert-favorite", notes = "")
    public void insertFavorite(@RequestBody String str)
    {
        favoriteEntity favorite = JSON.parseObject(str, favoriteEntity.class);
        this.service.insertFavorite(favorite);
    }
}
